﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlThayDoiTTBA : System.Web.UI.UserControl
{
    BCDataCenter.DataAccessLayer.DAL dt;
    MethodClassCommon common;
    DataTable tinh;
    DataSet ds, salekey, thuhuong;
    string error = "";
    static bool flag = false;
    static int sThuHuong = 0;
    SqlConnection con;
    SqlTransaction tran;
    SqlCommand Insert_FullTime;
    SqlCommand UpdateRecDate, UPDATE_TEAMLEADER, UPDATE_ADNCODE_KHIXOATVV, UPDATE_DEEPLEVEL, UPDATE_RECOMMNDER, DELETE_SALESP, Update_Introduce1, Update_ADNCode, UPDATE_ARSALESP, comINSERT_ARSALESP, comINSERT_TCARCUST, update_ARCOMPETITOR, UPDATE_LEVEL, get_introduce;
    private string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            HienThiTinh();
            HienThiQuan();
            NoiCap();
            trinhdo();

        }

    }
    void NoiCap()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        tinh = dt.get_data_table("GET_DATA_PROVINCE");
        DropNoiCap.DataSource = tinh;
        DropNoiCap.DataTextField = "ProvinceName";
        DropNoiCap.DataValueField = "ProvinceCode";
        DropNoiCap.DataBind();

    }
    void trinhdo()
    {
        for (int i = 1; i < 13; i++)
        {
            DropTrinhDo.Items.Add(Convert.ToString(i) + "/12");
        }
        DropTrinhDo.SelectedIndex = DropTrinhDo.Items.Count - 1;
    }
    void HienThiTinh()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        tinh = dt.get_data_table("GET_DATA_PROVINCE");
        DropTinh.DataSource = tinh;
        DropTinh.DataTextField = "ProvinceName";
        DropTinh.DataValueField = "ProvinceCode";
        DropTinh.DataBind();
    }
    void HienThiQuan()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        ds = dt.MyExecuteQuery(ref error, "GET_DATA_DISTRICTOWN_PROVINCECODE", CommandType.StoredProcedure, new SqlParameter("@ProvinceCode", DropTinh.SelectedValue.ToString()));
        DropQuanHuyen.DataSource = ds.Tables[0];
        DropQuanHuyen.DataTextField = "TownName";
        DropQuanHuyen.DataValueField = "TownCode";
        DropQuanHuyen.DataBind();
    }
    void clear()
    {
        txtRootAddress.Text = "";
        txtMaTuVanVien.Text = "";
        txtTenTuVanVien.Text = "";
        txtMaNguoiGioiThieu.Text = "";
        txtTenNguoiGioiThieu.Text = "";
        txtMaNguoiThuHuong.Text = "";
        txtTenNguoiThuHuong.Text = "";
        txtSoHopDong.Text = "";
        txtNgayKy.Text = "";
        txtSoTaiKhoan.Text = "";
        txtNganHang.Text = "";
        CheckConBanHang.Checked = false;
        txtSoCMND.Text = "";
        txtNgayCap.Text = "";
        txtNgaySinh.Text = "";
        CheckGioiTinh.Checked = false;
        txtSoNha.Text = "";
        txtTenDuong.Text = "";
        txtDtCoDinh.Text = "";
        txtDTDiDong.Text = "";
        txtEmail.Text = "";
        txtNgheNghiep.Text = "";
        CheckFullTime.Checked = true;
    }
    void text()
    {
        common = new MethodClassCommon();
        txtTenNguoiGioiThieu.Text = common.GetDataDepend("get_Recommender", "salesp_key", txtMaNguoiGioiThieu.Text);
    }
    void mthuhuong()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        thuhuong = dt.MyExecuteQuery(ref error, "SEARCH_Introduce_ARSALESP", CommandType.StoredProcedure, new SqlParameter("@parameter", txtSearch.Text), new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
        if (sThuHuong == 1)
        {
            if (thuhuong.Tables[0].Rows.Count == 1)
            {
                common = new MethodClassCommon();
                txtTenNguoiThuHuong.Text = common.GetDataDepend("get_Introduce", "salesp_key", thuhuong.Tables[0].Rows[0][0].ToString());
                txtMaNguoiThuHuong.Text = thuhuong.Tables[0].Rows[0][0].ToString();
                TabContainer1.ActiveTabIndex = 0;
                sThuHuong = 0;
            }
            else
            {
                GridView2.DataSource = thuhuong.Tables[0];
                GridView2.DataBind();
            }
        }
    }
    void get_introduceName(string maNguoiThuHuong)
    {
        common = new MethodClassCommon();
        try
        {
            txtTenNguoiThuHuong.Text = common.GetDataDepend("get_Introduce", "salesp_key", maNguoiThuHuong);
        }
        catch
        {
            txtTenNguoiThuHuong.Text = "";
        }
    }
    void grid()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        try
        {
            if (flag == true)
            {
                salekey = dt.MyExecuteQuery(ref error, "SEARCH_ARCOMPETITOR_CONDITION", CommandType.StoredProcedure, new SqlParameter("@parameter", txtSearch.Text), new SqlParameter("@sitecode", Session["Sitecode"].ToString()));
            }
            else
            {
                salekey = dt.MyExecuteQuery(ref error, "SEARCH_ARSALESP_CONDITION", CommandType.StoredProcedure, new SqlParameter("@parameter", txtSearch.Text), new SqlParameter("@sitecode", Session["Sitecode"].ToString()));
            }
            if (salekey.Tables[0].Rows.Count == 1)
            {
                txtNgayKy.Text = DateTime.Now.Date.ToString();
                txtMaTuVanVien.Text = salekey.Tables[0].Rows[0][0].ToString();
                txtTenTuVanVien.Text = salekey.Tables[0].Rows[0][1].ToString();
                txtMaNguoiGioiThieu.Text = salekey.Tables[0].Rows[0][3].ToString();
                if (salekey.Tables[0].Rows[0][2].ToString() == "True")
                {
                    CheckGioiTinh.Checked = true;
                }
                else
                {
                    CheckGioiTinh.Checked = false;
                }
                txtSoCMND.Text = salekey.Tables[0].Rows[0][4].ToString();
                txtNgayCap.Text = salekey.Tables[0].Rows[0][5].ToString();
                try
                {
                    DropNoiCap.SelectedValue = salekey.Tables[0].Rows[0][6].ToString();
                }
                catch
                {
                    DropNoiCap.SelectedIndex = -1;
                }
                txtNgaySinh.Text = salekey.Tables[0].Rows[0][7].ToString();
                txtSoNha.Text = salekey.Tables[0].Rows[0][8].ToString();
                try
                {
                    DropTinh.SelectedValue = salekey.Tables[0].Rows[0][9].ToString();
                }
                catch
                {
                    DropTinh.SelectedIndex = -1;
                }
                HienThiQuan();
                try
                {
                    DropQuanHuyen.SelectedValue = salekey.Tables[0].Rows[0][10].ToString();
                }
                catch
                {
                    DropQuanHuyen.SelectedIndex = -1;
                }
                txtDtCoDinh.Text = salekey.Tables[0].Rows[0][11].ToString();
                txtDTDiDong.Text = salekey.Tables[0].Rows[0][12].ToString();
                try
                {
                    DropTrinhDo.SelectedValue = salekey.Tables[0].Rows[0][13].ToString();
                }
                catch
                {
                    DropTrinhDo.SelectedIndex = DropTrinhDo.Items.Count - 1;
                }
                txtEmail.Text = salekey.Tables[0].Rows[0][14].ToString();
                txtNgheNghiep.Text = salekey.Tables[0].Rows[0][15].ToString();
                txtTenDuong.Text = salekey.Tables[0].Rows[0][16].ToString();
                CheckConBanHang.Checked = true;
                try
                {
                    txtMaNguoiThuHuong.Text = salekey.Tables[0].Rows[0][18].ToString();
                    get_introduceName(txtMaNguoiThuHuong.Text);
                }
                catch
                {
                    txtMaNguoiThuHuong.Text = "";
                }
                text();
                txtNgayKy.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
                if (flag == false)
                {
                    try
                    {
                        txtSoHopDong.Text = salekey.Tables[0].Rows[0][19].ToString();
                    }
                    catch
                    {
                        txtSoHopDong.Text = "Chưa có";
                    }
                    try
                    {
                        txtNgayKy.Text = salekey.Tables[0].Rows[0][20].ToString();
                    }
                    catch
                    {
                        txtNgayKy.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
                    }
                    txtSoTaiKhoan.Text = salekey.Tables[0].Rows[0][21].ToString();
                    txtNganHang.Text = salekey.Tables[0].Rows[0][22].ToString();
                    if (salekey.Tables[0].Rows[0][17].ToString() == "True")
                    {
                        CheckConBanHang.Checked = true;
                    }
                    else
                    {
                        CheckConBanHang.Checked = false;
                    }
                    switch (Convert.ToInt32(salekey.Tables[0].Rows[0][23].ToString()))
                    {
                        case 1:
                            txtCapBac.Text = "Tư vấn viên";
                            break;
                        case 2:
                            txtCapBac.Text = "Trưởng nhóm(BUM)";
                            break;
                        case 3:
                            txtCapBac.Text = "Trưởng nhóm cấp cao(SUM)";
                            break;
                        case 4:
                            txtCapBac.Text = "Quản lý kinh doanh(ESM))";
                            break;
                        case 5:
                            txtCapBac.Text = "Quản lý kinh doanh cấp cao(ADM)";
                            break;
                        case 6:
                            txtCapBac.Text = "Trưởng ban kinh doanh(DM)";
                            break;
                    }
                    if (salekey.Tables[0].Rows[0][17].ToString() == "True")
                    {
                        CheckConBanHang.Checked = true;
                    }
                    else
                    {
                        CheckConBanHang.Checked = false;
                    }
                    if (salekey.Tables[0].Rows[0]["FullTime"].ToString() == "True")
                    {
                        CheckFullTime.Checked = true;
                    }
                    else
                    {
                        CheckFullTime.Checked = false;
                    }
                    txtRootAddress.Text = salekey.Tables[0].Rows[0]["rootAddress"].ToString();
                }
                else
                {
                    CheckConBanHang.Checked = true;
                    txtCapBac.Text = "Tư vấn viên";
                }
                TabContainer1.ActiveTabIndex = 0;
            }
            else
            {
                GridView2.DataSource = salekey.Tables[0];
                GridView2.DataBind();
            }
            salekey.Dispose();
        }
        catch
        {
            return;
        }
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            dt = new BCDataCenter.DataAccessLayer.DAL();
            if (sThuHuong == 0)
            {
                try
                {
                    if (flag == true)
                    {
                        salekey = dt.MyExecuteQuery(ref error, "SEARCH_ARCOMPETITOR_CONDITION", CommandType.StoredProcedure, new SqlParameter("@parameter", grv.Cells[0].Text), new SqlParameter("@sitecode", Session["Sitecode"].ToString()));
                    }
                    else if (flag == false)
                    {
                        salekey = dt.MyExecuteQuery(ref error, "SEARCH_ARSALESP_CONDITION", CommandType.StoredProcedure, new SqlParameter("@parameter", grv.Cells[0].Text), new SqlParameter("@sitecode", Session["Sitecode"].ToString()));

                    }
                    txtMaTuVanVien.Text = salekey.Tables[0].Rows[0][0].ToString();
                    txtTenTuVanVien.Text = salekey.Tables[0].Rows[0][1].ToString();
                    txtMaNguoiGioiThieu.Text = salekey.Tables[0].Rows[0][3].ToString();
                    if (salekey.Tables[0].Rows[0][2].ToString() == "True")
                    {
                        CheckGioiTinh.Checked = true;
                    }
                    else
                    {
                        CheckGioiTinh.Checked = false;
                    }
                    txtSoCMND.Text = salekey.Tables[0].Rows[0][4].ToString();
                    txtNgayCap.Text = salekey.Tables[0].Rows[0][5].ToString();
                    try
                    {
                        DropNoiCap.SelectedValue = salekey.Tables[0].Rows[0][6].ToString();
                    }
                    catch
                    {
                        DropNoiCap.SelectedIndex = -1;
                    }
                    txtNgaySinh.Text = salekey.Tables[0].Rows[0][7].ToString();
                    txtSoNha.Text = salekey.Tables[0].Rows[0][8].ToString();
                    try
                    {
                        DropTinh.SelectedValue = salekey.Tables[0].Rows[0][9].ToString();
                    }
                    catch
                    {
                        DropTinh.SelectedIndex = -1;
                    }
                    HienThiQuan();
                    try
                    {
                        DropQuanHuyen.SelectedValue = salekey.Tables[0].Rows[0][10].ToString();
                    }
                    catch
                    {
                        DropQuanHuyen.SelectedIndex = -1;
                    }
                    txtDtCoDinh.Text = salekey.Tables[0].Rows[0][11].ToString();
                    txtDTDiDong.Text = salekey.Tables[0].Rows[0][12].ToString();
                    try
                    {
                        DropTrinhDo.SelectedValue = Convert.ToString(Convert.ToInt32(salekey.Tables[0].Rows[0][14].ToString()) - 1);
                    }
                    catch
                    {
                        DropTrinhDo.SelectedIndex = DropTrinhDo.Items.Count - 1;
                    }
                    txtEmail.Text = salekey.Tables[0].Rows[0][14].ToString();
                    txtNgheNghiep.Text = salekey.Tables[0].Rows[0][15].ToString();
                    txtTenDuong.Text = salekey.Tables[0].Rows[0][16].ToString();
                    try
                    {
                        txtMaNguoiThuHuong.Text = salekey.Tables[0].Rows[0][18].ToString();
                        get_introduceName(txtMaNguoiThuHuong.Text);
                    }
                    catch
                    {
                        txtMaNguoiThuHuong.Text = "";
                    }
                    text();
                    CheckConBanHang.Checked = true;
                    txtCapBac.Text = "Tư vấn viên";
                    txtNgayKy.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
                    if (flag == false)
                    {
                        try
                        {
                            txtSoHopDong.Text = salekey.Tables[0].Rows[0][19].ToString();
                        }
                        catch
                        {
                            txtSoHopDong.Text = "Chưa có";
                        }
                        try
                        {
                            txtNgayKy.Text = salekey.Tables[0].Rows[0][20].ToString();
                        }
                        catch
                        {
                            txtNgayKy.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
                        }
                        txtSoTaiKhoan.Text = salekey.Tables[0].Rows[0][21].ToString();
                        txtNganHang.Text = salekey.Tables[0].Rows[0][22].ToString();
                        switch (Convert.ToInt32(salekey.Tables[0].Rows[0][23].ToString()))
                        {
                            case 1:
                                txtCapBac.Text = "Tư vấn viên";
                                break;
                            case 2:
                                txtCapBac.Text = "Trưởng nhóm(BUM)";
                                break;
                            case 3:
                                txtCapBac.Text = "Trưởng nhóm cấp cao(SUM)";
                                break;
                            case 4:
                                txtCapBac.Text = "Quản lý kinh doanh(ESM))";
                                break;
                            case 5:
                                txtCapBac.Text = "Quản lý kinh doanh cấp cao(ADM)";
                                break;
                            case 6:
                                txtCapBac.Text = "Trưởng ban kinh doanh(DM)";
                                break;
                        }
                        if (salekey.Tables[0].Rows[0][17].ToString() == "True")
                        {
                            CheckConBanHang.Checked = true;
                        }
                        else
                        {
                            CheckConBanHang.Checked = false;
                        }
                        if (salekey.Tables[0].Rows[0]["Fulltime"].ToString() == "True")
                        {
                            CheckFullTime.Checked = true;
                        }
                        else
                        {
                            CheckFullTime.Checked = false;
                        }
                        txtRootAddress.Text = salekey.Tables[0].Rows[0]["rootAddress"].ToString();
                    }

                }
                catch (Exception ex)
                {
                    uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
                }
            }
            else
            {
                common = new MethodClassCommon();
                try
                {
                    txtTenNguoiThuHuong.Text = common.GetDataDepend("get_Introduce", "salesp_key", grv.Cells[0].Text);
                    txtMaNguoiThuHuong.Text = grv.Cells[0].Text;
                    sThuHuong = 0;
                }
                catch
                {
                    return;
                }
            }
            TabContainer1.ActiveTabIndex = 0;
            GridView2.DataSource = null;
            GridView2.DataBind();
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        if (sThuHuong == 1)
        {
            mthuhuong();
        }
        else
        {
            grid();
        }
    }
    protected void ImgNew_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            sThuHuong = 0;

            flag = true;

            lblTuVan.Text = "tư vấn viên mới";
            ImgTimTuVanVienMoi.Visible = true;
            TabContainer1.ActiveTabIndex = 0;
            clear();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    public string GetngayChuachot()
    {
        //lay ngay chua chot 
        common = new MethodClassCommon();
        string ClosedDate = common.GetDataDepend("get_CloseDate", "Sitecode", Session["sitecode"].ToString().Trim());
        DateTime dateTestngayChot;
        System.Globalization.DateTimeFormatInfo sFormatngaychot = new System.Globalization.DateTimeFormatInfo();
        sFormatngaychot.ShortDatePattern = "dd/MM/yyyy";
        dateTestngayChot = Convert.ToDateTime(ClosedDate, sFormatngaychot);
        string ngaychuachot = dateTestngayChot.Date.AddDays(1).ToString("dd/MM/yyyy");
        return ngaychuachot;

    }
    protected void ImgSave_Click(object sender, ImageClickEventArgs e)
    {
        int kq = 0;

        if (flag == true)
        {
            string closedate = GetngayChuachot();
            con = new SqlConnection(str);
            con.Open();
            tran = con.BeginTransaction();

            comINSERT_ARSALESP = new SqlCommand();
            comINSERT_ARSALESP.Connection = con;
            comINSERT_TCARCUST = new SqlCommand();
            comINSERT_TCARCUST.Connection = con;
            update_ARCOMPETITOR = new SqlCommand();
            update_ARCOMPETITOR.Connection = con;
            UPDATE_LEVEL = new SqlCommand();
            UPDATE_LEVEL.Connection = con;
            Update_ADNCode = new SqlCommand();
            Update_ADNCode.Connection = con;
            Update_Introduce1 = new SqlCommand();
            Update_Introduce1.Connection = con;
            UPDATE_TEAMLEADER = new SqlCommand();
            UPDATE_TEAMLEADER.Connection = con;
            Insert_FullTime = new SqlCommand();
            Insert_FullTime.Connection = con;
            UpdateRecDate = new SqlCommand();
            UpdateRecDate.Connection = con;
            try
            {
                comINSERT_ARSALESP.Transaction = tran;
                comINSERT_ARSALESP.CommandText = "INSERT_ARSALESP";
                comINSERT_ARSALESP.CommandType = CommandType.StoredProcedure;
                comINSERT_ARSALESP.Parameters.Add(new SqlParameter("@vSalesp_key", txtMaTuVanVien.Text));
                comINSERT_ARSALESP.ExecuteNonQuery();

                comINSERT_TCARCUST.Transaction = tran;
                comINSERT_TCARCUST.CommandText = "INSERT_TCARCUST";
                comINSERT_TCARCUST.CommandType = CommandType.StoredProcedure;
                comINSERT_TCARCUST.Parameters.Add(new SqlParameter("@vSalesp_key", txtMaTuVanVien.Text));
                comINSERT_TCARCUST.ExecuteNonQuery();

                update_ARCOMPETITOR.Transaction = tran;
                update_ARCOMPETITOR.CommandText = "UPDATE_ARCOMPETITOR_ISUSED_RECOMENDER";
                update_ARCOMPETITOR.CommandType = CommandType.StoredProcedure;
                update_ARCOMPETITOR.Parameters.Add(new SqlParameter("@vSalesp_key", txtMaTuVanVien.Text));
                update_ARCOMPETITOR.ExecuteNonQuery();

                UPDATE_LEVEL.Transaction = tran;
                UPDATE_LEVEL.CommandText = "UPDATE_LEVEL";
                UPDATE_LEVEL.CommandType = CommandType.StoredProcedure;
                UPDATE_LEVEL.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
                UPDATE_LEVEL.Parameters.Add(new SqlParameter("@nLevel", 1));
                UPDATE_LEVEL.ExecuteNonQuery();

                Update_ADNCode.Transaction = tran;
                Update_ADNCode.CommandText = "UPDATE_ADNCODE";
                Update_ADNCode.CommandType = CommandType.StoredProcedure;
                Update_ADNCode.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
                Update_ADNCode.ExecuteNonQuery();

                Update_Introduce1.Transaction = tran;
                Update_Introduce1.CommandText = "UPDATE_INTRODUCE";
                Update_Introduce1.CommandType = CommandType.StoredProcedure;
                Update_Introduce1.Parameters.Add(new SqlParameter("@vSalesp_key", txtMaTuVanVien.Text));
                Update_Introduce1.Parameters.Add(new SqlParameter("@introduce", txtMaNguoiThuHuong.Text));
                Update_Introduce1.ExecuteNonQuery();

                UPDATE_TEAMLEADER.Transaction = tran;
                UPDATE_TEAMLEADER.CommandText = "UPDATE_TEAMLEADER";
                UPDATE_TEAMLEADER.CommandType = CommandType.StoredProcedure;
                UPDATE_TEAMLEADER.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
                UPDATE_TEAMLEADER.ExecuteNonQuery();

                Insert_FullTime.Transaction = tran;
                Insert_FullTime.CommandText = "Insert_FullTime";
                Insert_FullTime.CommandType = CommandType.StoredProcedure;
                Insert_FullTime.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
                if (CheckFullTime.Checked)
                {
                    Insert_FullTime.Parameters.Add(new SqlParameter("@FullTime", "True"));
                }
                else
                {
                    Insert_FullTime.Parameters.Add(new SqlParameter("@FullTime", "False"));
                }
                Insert_FullTime.ExecuteNonQuery();

                UpdateRecDate.Transaction = tran;
                UpdateRecDate.CommandText = "UpdateRecDate";
                UpdateRecDate.CommandType = CommandType.StoredProcedure;
                UpdateRecDate.Parameters.Add(new SqlParameter("@salesp_key", txtMaTuVanVien.Text));
                UpdateRecDate.Parameters.Add(new SqlParameter("@RootAddress", txtRootAddress.Text));
                UpdateRecDate.Parameters.Add(new SqlParameter("@date", closedate));
                UpdateRecDate.ExecuteNonQuery();

                tran.Commit();
                uscMsgBox1.AddMessage("Hoàn thành thêm tư vấn viên", uscMsgBox.enmMessageType.Success);
                flag = false;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Success);
            }
            finally
            {
                con.Close();
            }
        }
        else
        {

            UPDATE_ARSALESP = new SqlCommand();
            common = new MethodClassCommon();
            SqlParameter salesp_key = new SqlParameter("@salesp_key", txtMaTuVanVien.Text);
            SqlParameter salesp_name = new SqlParameter("@salesPName", txtTenTuVanVien.Text);
            SqlParameter Introduce = new SqlParameter("@Introduce", txtMaNguoiThuHuong.Text.Trim());
            SqlParameter ContractID = new SqlParameter("@ContractID", txtSoHopDong.Text);
            SqlParameter ContractDate = new SqlParameter("@ContractDate", txtNgayKy.Text);
            SqlParameter BankAcc = new SqlParameter("@BankAcc", txtSoTaiKhoan.Text);
            SqlParameter BankName = new SqlParameter("@BankName", txtNganHang.Text);
            SqlParameter IDCard = new SqlParameter("@IDCard", txtSoCMND.Text);
            SqlParameter IDDate = new SqlParameter("@IDDate", txtNgayCap.Text);
            SqlParameter IDWhere = new SqlParameter("@IDWhere", DropNoiCap.SelectedValue.ToString());
            SqlParameter BirthDate = new SqlParameter("@BirthDate", txtNgaySinh.Text);
            SqlParameter Sex;
            if (CheckGioiTinh.Checked)
            {
                Sex = new SqlParameter("@Sex", "True");
            }
            else
            {
                Sex = new SqlParameter("@Sex", "False");
            }
            SqlParameter FullTime;
            if (CheckFullTime.Checked)
            {
                FullTime = new SqlParameter("@FullTime", "True");
            }
            else
            {
                FullTime = new SqlParameter("@FullTime", "False");
            }
            SqlParameter Address = new SqlParameter("@Address", txtSoNha.Text);
            SqlParameter Street = new SqlParameter("@Street", txtTenDuong.Text);
            SqlParameter Province = new SqlParameter("@Province", DropTinh.SelectedValue.ToString());
            SqlParameter Town = new SqlParameter("@Town", DropQuanHuyen.SelectedValue.ToString());
            SqlParameter Tel = new SqlParameter("@Tel", txtDtCoDinh.Text);
            SqlParameter Mobile = new SqlParameter("@Mobile", txtDTDiDong.Text);
            string trinhdo = DropTrinhDo.SelectedItem.ToString();
            SqlParameter EducationLevel = new SqlParameter("@EducationLevel", trinhdo);
            SqlParameter Email = new SqlParameter("@Email", txtEmail.Text);
            SqlParameter Job = new SqlParameter("@Job", txtNgheNghiep.Text);
            SqlParameter rootAddress = new SqlParameter("@rootAddress", txtRootAddress.Text);
            kq = common.Update_Data("UPDATE_ARSALESP", CommandType.StoredProcedure, ref error, salesp_key,salesp_name, Introduce, ContractID, ContractDate, BankAcc, BankName, IDCard, IDDate, IDWhere, BirthDate, Sex, Address, Street, Province, Town, Tel, Mobile, EducationLevel, Email, Job, FullTime, rootAddress);
            if (kq == 0)
            {
                uscMsgBox1.AddMessage("Cập nhật dữ liệu thành công", uscMsgBox.enmMessageType.Success);
                flag = false;
            }
            else
            {
                uscMsgBox1.AddMessage("Mã tư vấn viên không tồn tại", uscMsgBox.enmMessageType.Error);
            }
        }
    }
    protected void DropTinh_SelectedIndexChanged1(object sender, EventArgs e)
    {
        HienThiQuan();
    }
    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        if (sThuHuong == 1)
        {
            lblTuVan.Text = "người thụ hưởng";
            mthuhuong();
        }
        else
        {
            grid();
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (sThuHuong == 1)
        {
            lblTuVan.Text = "người thụ hưởng";
            mthuhuong();
        }
        else
        {
            grid();
        }
    }
    protected void ImgTimTuVanVienMoi_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            sThuHuong = 0;
            lblTuVan.Text = "tư vấn viên mới";
            TabContainer1.ActiveTabIndex = 1;
            flag = true;
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void ImgTimKiemTuVanVienChinhThuc_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            sThuHuong = 0;
            lblTuVan.Text = "tư vấn viên chính thức...";

            TabContainer1.ActiveTabIndex = 1;
            lblTuVan.Text = "tư vấn viên chính thức";
            ImgTimTuVanVienMoi.Visible = false;
            flag = false;
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (flag == false)
            {
                ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
                bool kq = obj.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_deleteBA]", ref error,
                    new SqlParameter("@selSalesPKey", txtMaTuVanVien.Text.Trim()));
                if (kq)
                {
                    uscMsgBox1.AddMessage("Cắt mã TVV thành công", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage("Cắt mã TVV thất bại", uscMsgBox.enmMessageType.Error);
                }
            }
            else
            {
                uscMsgBox1.AddMessage("Phải chọn đúng mã tư vấn viên chính thức đang tồn tại", uscMsgBox.enmMessageType.Error);

            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message + error, method);
        }
    }
    protected void txtMaNguoiThuHuong_TextChanged(object sender, EventArgs e)
    {
        try
        {

            txtTenNguoiThuHuong.Text = txtMaNguoiThuHuong.Text.Substring(txtMaNguoiThuHuong.Text.IndexOf(Convert.ToChar("|")) + 2);
            txtMaNguoiThuHuong.Text = txtMaNguoiThuHuong.Text.Substring(0, txtMaNguoiThuHuong.Text.IndexOf(Convert.ToChar("|")));
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }

    }
    protected void ImgTimNguoiThuHuong_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            TabContainer1.ActiveTabIndex = 1;
            sThuHuong = 1;
            lblTuVan.Text = "người thụ hưởng";
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
}
